package com.baway.zhangcaiping.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.zhangcaiping.Main2Activity;
import com.baway.zhangcaiping.R;
import com.baway.zhangcaiping.bean.Bean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/6.
 */

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

    LayoutInflater inflater;
    ArrayList<Bean.ResultBean.RowsBean.InfoBean> arr;

    //构造器
    public RecycleAdapter(Context context, ArrayList<Bean.ResultBean.RowsBean.InfoBean> arr) {
        this.inflater=LayoutInflater.from(context);
        this.arr=arr;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //找布局
        View view = inflater.inflate(R.layout.recycle_item, parent, false);
        //创建持有类对象
        MyViewHolder mv=new MyViewHolder(view);
        return mv;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        //赋值
        holder.tv_address.setText(arr.get(position).address);
        holder.tv_loupan_name.setText(arr.get(position).loupan_name);
        holder.tv_price.setText(arr.get(position).price+"元/m²");
        holder.tv_sale_title.setText(arr.get(position).sale_title);
        holder.tv_tags.setText(arr.get(position).tags);
        ImageLoader.getInstance().displayImage(arr.get(position).default_image,holder.image, Main2Activity.options);

    }

    @Override
    public int getItemCount() {
        return arr.size();
    }

    //持有类
    class MyViewHolder extends RecyclerView.ViewHolder
    {

        private final ImageView image;
        private final TextView tv_address;
        private final TextView tv_loupan_name;
        private final TextView tv_price;
        private final TextView tv_sale_title;
        private final TextView tv_tags;

        public MyViewHolder(View itemView) {
            super(itemView);
            //找控件
            image = (ImageView) itemView.findViewById(R.id.image);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
            tv_loupan_name = (TextView) itemView.findViewById(R.id.tv_loupan_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_sale_title = (TextView) itemView.findViewById(R.id.tv_sale_title);
            tv_tags = (TextView) itemView.findViewById(R.id.tv_tags);
        }
    }
}
